package com.itboye.sunsun.message.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.sunsun.luntan.fragment.WoDeZhuTiFragment;
import com.itboye.sunsun.me.ui.KeFuMeFragment;
import com.itboye.sunsun.me.ui.MessageXiTongFragmet;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsunhome.R;

public class MyMessageActivity extends BaseActivity implements OnClickListener {

	LinearLayout layoutMycollTiezixitong;
	LinearLayout layoutTieZiHuiFukefu;
	LinearLayout layoutTieluntan;
	LinearLayout content_layoutMycoll;

	TextView add_shap_title_tv;

	// 头部标签切换的Fragment
	private Fragment XiTongFragmet, payFragment, currentFragment;
	Button btnTiezitongzhi, btnTieziHuifukefu;

	private View viewright, viewLeift;

	ImageView close_icon;
/**
 * 英文状态下
 */
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.fragment_regongkefu);

		initTab();
		setStatusBarColor(R.color.home_blue);
		add_shap_title_tv.setText(R.string.personal_meaage);
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layoutMycollTiezixitong:
			clickTab1Layout();
			break;
		case R.id.layoutTieZiHuiFukefu:
			clickTab2Layout();
			break;
		case R.id.btnTiezitongzhi:
			clickTab1Layout();
			break;
		case R.id.btnTieziHuifukefu:
			clickTab2Layout();
			break;

		default:
			break;
		}
	}

	/**
	 * 初始化头部标签
	 */
	@SuppressWarnings("deprecation")
	private void initTab() {
		if (XiTongFragmet == null) {
			XiTongFragmet = new MessageXiTongFragmet();
		}

		if (!XiTongFragmet.isAdded()) {
			// 提交事务
			getSupportFragmentManager().beginTransaction()
					.add(R.id.content_layoutMycoll, XiTongFragmet).commit();

			// 记录当前Fragment
			currentFragment = XiTongFragmet;
			// 设置图片文本的变化
			btnTiezitongzhi.setTextColor(getResources().getColor(
					R.color.home_blue));
			btnTieziHuifukefu.setTextColor(getResources()
					.getColor(R.color.gray));

			viewright.setBackgroundColor(getResources().getColor(
					R.color.home_blue));// (R.color.home_blue);
			viewLeift.setBackgroundColor(getResources().getColor(
					R.color.backgroundColor));

		}
	}

	private void clickTab1Layout() {
		if (XiTongFragmet == null) {
			XiTongFragmet = new WoDeZhuTiFragment();
		}
		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				XiTongFragmet);

		// 设置底部tab变化
		btnTiezitongzhi
				.setTextColor(getResources().getColor(R.color.home_blue));
		btnTieziHuifukefu.setTextColor(getResources().getColor(R.color.gray));

		viewright
				.setBackgroundColor(getResources().getColor(R.color.home_blue));// (R.color.home_blue);
		viewLeift.setBackgroundColor(getResources().getColor(
				R.color.backgroundColor));
	}

	/**
	 * 点击第二个tab
	 */
	private void clickTab2Layout() {
		if (payFragment == null) {
			payFragment = new KeFuMeFragment();
		}
		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				payFragment);

		btnTieziHuifukefu.setTextColor(getResources().getColor(
				R.color.home_blue));
		btnTiezitongzhi.setTextColor(getResources().getColor(R.color.gray));

		viewright.setBackgroundColor(getResources().getColor(
				R.color.backgroundColor));// (R.color.home_blue);
		viewLeift
				.setBackgroundColor(getResources().getColor(R.color.home_blue));
	}

	/**
	 * 添加或者显示碎片
	 * 
	 * @param transaction
	 * @param fragment
	 */
	private void addOrShowFragment(FragmentTransaction transaction,
			Fragment fragment) {
		if (currentFragment == fragment)
			return;

		if (!fragment.isAdded()) { // 如果当前fragment未被添加，则添加到Fragment管理器中
			transaction.hide(currentFragment)
					.add(R.id.content_layoutMycoll, fragment).commit();
		} else {
			transaction.hide(currentFragment).show(fragment).commit();
		}

		currentFragment = fragment;
	}

}
